package com.eight.paper.admin.controller;

import com.eight.common.annotation.Log;
import com.eight.common.constant.UserConstants;
import com.eight.common.core.controller.BaseController;
import com.eight.common.core.domain.AjaxResult;
import com.eight.common.enums.BusinessType;
import com.eight.common.utils.StringUtils;
import com.eight.common.core.domain.entity.QuestionRepo;
import com.eight.paper.admin.service.IQuestionRepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 试题题库章节Controller
 * 
 * @author eight
 * @date 2023-10-11
 */
@Api(value = "题库章节管理", tags = "题库章节管理")
@RestController
@RequestMapping("/paper/repo")
public class QuestionRepoController extends BaseController
{
    @Autowired
    private IQuestionRepoService questionRepoService;

    /**
     * 查询试题题库章节列表
     */
    @ApiOperation(value = "查询试题题库章节列表")
    @PreAuthorize("@ss.hasPermi('paper:repo:list')")
    @GetMapping("/list")
    public AjaxResult list(QuestionRepo questionRepo)
    {
        List<QuestionRepo> list = questionRepoService.selectQuestionRepoList(questionRepo);
        return success(list);
    }

    /**
     * 获取试题题库章节详细信息
     */
    @ApiOperation(value = "获取试题题库章节详细信息")
    @PreAuthorize("@ss.hasPermi('paper:repo:query')")
    @GetMapping(value = "/{repoId}")
    public AjaxResult getInfo(@PathVariable("repoId") Long repoId)
    {
        return success(questionRepoService.selectQuestionRepoByRepoId(repoId));
    }

    /**
     * 新增试题题库章节
     */
    @ApiOperation(value = "新增试题题库章节")
    @PreAuthorize("@ss.hasPermi('paper:repo:add')")
   @Log(title = "试题题库章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionRepo questionRepo)
    {
        if (!questionRepoService.checkRepoNameUnique(questionRepo))
        {
            return error("新增题库章节'" + questionRepo.getRepoName() + "'失败，名称已存在");
        }
        questionRepo.setCreateBy("1");
        return toAjax(questionRepoService.insertQuestionRepo(questionRepo));
    }

    /**
     * 修改试题题库章节
     */
    @ApiOperation(value = "修改试题题库章节")
    @PreAuthorize("@ss.hasPermi('paper:repo:edit')")
    @Log(title = "试题题库章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionRepo questionRepo)
    {
        Long repoId = questionRepo.getRepoId();
        if (!questionRepoService.checkRepoNameUnique(questionRepo))
        {
            return error("修改题库章节'" + questionRepo.getRepoName() + "'失败，题库章节名称已存在");
        }
        else if (questionRepo.getParentId().equals(repoId))
        {
            return error("修改题库章节'" + questionRepo.getRepoName() + "'失败，上级题库章节不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, questionRepo.getStatus()) && questionRepoService.selectNormalChildrenRepoById(repoId) > 0)
        {
            return error("该题库章节包含未停用的子题库章节！");
        }
        return toAjax(questionRepoService.updateQuestionRepo(questionRepo));
    }

    /**
     * 删除试题题库章节
     */
    @ApiOperation(value = "删除试题题库章节")
    @PreAuthorize("@ss.hasPermi('paper:repo:remove')")
    @Log(title = "试题题库章节", businessType = BusinessType.DELETE)
	@DeleteMapping("/{repoId}")
    public AjaxResult remove(@PathVariable Long repoId)
    {
        if (questionRepoService.hasChildByRepoId(repoId))
        {
            return warn("存在下级题库章节,不允许删除");
        }
        if (questionRepoService.checkRepotExistQuestion(repoId))
        {
            return warn("题库章节存在试题,不允许删除");
        }
        return toAjax(questionRepoService.deleteQuestionRepoByRepoId(repoId));
    }
}
